package com.thkj.common.util;

import java.io.IOException;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.thkj.common.config.AppConfig;
import com.thkj.modules.check.entity.TblCheck;
import com.thkj.modules.check.entity.TblInformation;
import com.thkj.modules.sys.entity.TblNotice;

import sun.misc.BASE64Encoder;

public class PushUtils {

	/**
	 * 极光给用户发推送 消息
	 *
	 * @param registrationId
	 * @param type
	 */
	public static String sendForJg(String registrationId, String type, TblInformation iDto) {
		String url = "https://bjapi.push.jiguang.cn/v3/push";
//        String url = "https://api.jpush.cn/v3/push";
		JSONObject json = new JSONObject();
		// platform推送平台
		json.put("platform", "all");
		// audience推送目标
		JSONArray registrationIdList = new JSONArray();
		registrationIdList.add(registrationId);
		JSONObject audience = new JSONObject();
		audience.put("registration_id", registrationIdList);
		json.put("audience", audience);
		// notification通知
		JSONObject notification = new JSONObject();
		// notification通知--android
		JSONObject android = new JSONObject();
		android.put("alert", iDto.getContent());
		android.put("title", iDto.getTitle());
		android.put("builder_id", 1);
		android.put("large_icon", "http://www.jiguang.cn/largeIcon.jpg");
		JSONObject intent = new JSONObject();
		intent.put("url", "intent:#Intent;component=com.jiguang.push/com.example.jpushdemo.SettingActivity;end");
		android.put("intent", intent);
		JSONObject extras = new JSONObject();
		extras.put("msg_id", iDto.getId());
		extras.put("msg_content", iDto.getId());
		extras.put("msg_type", 1);
		android.put("extras", extras);
		notification.put("android", android);
		// notification通知--ios
		JSONObject ios = new JSONObject();
		ios.put("alert", iDto.getContent());
		ios.put("sound", "default");
		ios.put("badge", "+1");
		ios.put("thread-id", "default");
		ios.put("extras", extras);
		notification.put("ios", ios);
		json.put("notification", notification);
		// 消息
		JSONObject message = new JSONObject();
		message.put("msg_content", iDto.getContent());
		message.put("content_type", "text");
		message.put("title", iDto.getTitle());
		message.put("extras", extras);
		json.put("message", message);
		// 推送当前用户不在线时，为该用户保留多长时间的离线消息（单位秒）
		JSONObject options = new JSONObject();
		options.put("time_to_live", 864000);
		options.put("apns_production", false);
		json.put("options", options);
//        System.out.println(json);
//        System.out.println(getAuthorization(type));
		String ret = HttpClientUtil.doPostJson(url, json.toJSONString(), getAuthorization(type));
		return ret;
	}

	/**
	 * 给所有人发
	 * 
	 * @param type
	 * @param noticeDto
	 * @return
	 */
	public static String sendForJgAll(String type, TblNotice noticeDto) {
		String url = "https://bjapi.push.jiguang.cn/v3/push";
		JSONObject json = new JSONObject();
		// platform推送平台
		json.put("platform", "all");
		// audience推送目标
		json.put("audience", "all");
		// notification通知
		JSONObject notification = new JSONObject();
		// notification通知--android
		JSONObject android = new JSONObject();
		android.put("alert", noticeDto.getContent());
		android.put("title", noticeDto.getTitle());
		android.put("builder_id", 1);
		android.put("large_icon", "http://www.jiguang.cn/largeIcon.jpg");
		JSONObject intent = new JSONObject();
		intent.put("url", "intent:#Intent;component=com.jiguang.push/com.example.jpushdemo.SettingActivity;end");
		android.put("intent", intent);
		JSONObject extras = new JSONObject();
		extras.put("msg_id", noticeDto.getId());
		extras.put("msg_content", noticeDto.getContent());
		extras.put("msg_type", 2);
		android.put("extras", extras);
		notification.put("android", android);
		// notification通知--ios
		JSONObject ios = new JSONObject();
		ios.put("alert", noticeDto.getContent());
		ios.put("sound", "default");
		ios.put("badge", "+1");
		ios.put("thread-id", "default");
		ios.put("extras", extras);
		notification.put("ios", ios);
		json.put("notification", notification);
		// 消息
		JSONObject message = new JSONObject();
		message.put("msg_content", noticeDto.getContent());
		message.put("content_type", "text");
		message.put("title", noticeDto.getTitle());
		message.put("extras", extras);
		json.put("message", message);
		// 推送当前用户不在线时，为该用户保留多长时间的离线消息（单位秒）
		JSONObject options = new JSONObject();
		options.put("time_to_live", 864000);
		options.put("apns_production", false);
		json.put("options", options);
		System.out.println(json);
		System.out.println(getAuthorization(type));
		String ret = HttpClientUtil.doPostJson(url, json.toJSONString(), getAuthorization(type));
		return ret;
	}

	/**
	 * 极光给用户发推送 消息
	 *
	 * @param registrationId
	 * @param type
	 */
	public static String sendCheckMsg(String registrationId, String type, TblCheck iDto) {
		String url = "https://bjapi.push.jiguang.cn/v3/push";
//        String url = "https://api.jpush.cn/v3/push";
		JSONObject json = new JSONObject();
		// platform推送平台
		json.put("platform", "all");
		// audience推送目标
		JSONArray registrationIdList = new JSONArray();
		registrationIdList.add(registrationId);
		JSONObject audience = new JSONObject();
		audience.put("registration_id", registrationIdList);
		json.put("audience", audience);
		// notification通知
		JSONObject notification = new JSONObject();
		// notification通知--android
		JSONObject android = new JSONObject();
		android.put("alert", iDto.getDescription());
		android.put("title", iDto.getCheckName());
		android.put("builder_id", 1);
		android.put("large_icon", "http://www.jiguang.cn/largeIcon.jpg");
		JSONObject intent = new JSONObject();
		intent.put("url", "intent:#Intent;component=com.jiguang.push/com.example.jpushdemo.SettingActivity;end");
		android.put("intent", intent);
		JSONObject extras = new JSONObject();
		extras.put("msg_id", iDto.getId());
		extras.put("msg_content", iDto.getId());
		extras.put("msg_type", 1);
		android.put("extras", extras);
		notification.put("android", android);
		// notification通知--ios
		JSONObject ios = new JSONObject();
		ios.put("alert", iDto.getDescription());
		ios.put("sound", "default");
		ios.put("badge", "+1");
		ios.put("thread-id", "default");
		ios.put("extras", extras);
		notification.put("ios", ios);
		json.put("notification", notification);
		// 消息
		JSONObject message = new JSONObject();
		message.put("msg_content", iDto.getDescription());
		message.put("content_type", "text");
		message.put("title", iDto.getCheckName());
		message.put("extras", extras);
		json.put("message", message);
		// 推送当前用户不在线时，为该用户保留多长时间的离线消息（单位秒）
		JSONObject options = new JSONObject();
		options.put("time_to_live", 864000);
		options.put("apns_production", false);
		json.put("options", options);
		String ret = HttpClientUtil.doPostJson(url, json.toJSONString(), getAuthorization(type));
		return ret;
	}

	public static String getAuthorization(String type) {
		String str = null;
		try {
			if (type.equals("jg")) {
				str = "Basic" + " " + base64Encode(AppConfig.APPKEY_JG + ":" + AppConfig.MASTERSECRET_JG);
			} else if (type.equals("jy")) {
				str = "Basic" + " " + base64Encode(AppConfig.APPKEY_JY + ":" + AppConfig.MASTERSECRET_JY);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}

	public static String base64Encode(String encodeText) throws IOException {
		BASE64Encoder encoder = new BASE64Encoder();
		String str = encoder.encode(encodeText.getBytes("UTF-8"));
		return str;
	}
}
